package cn.itcast.bos.web.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.hql.ast.tree.FromElement;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.Staff;
import cn.itcast.bos.domain.pagebean.PageBean;
import cn.itcast.bos.web.action.base.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@Scope("prototype")
public class StaffAction extends BaseAction<Staff>{
/*	@Resource
	private PageBean pageBean;
	*/
	//保存staff
	public String saveStaff(){
		staffService.saveStaff(model);
		return "listStaff";
	}
	
	
	//pageQueryList分页查询
	// 使用 属性封装数据
	private int rows;
	private int page;
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String pageQueryList() throws IOException{
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(page);
		pageBean.setPageSize(rows);
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Staff.class);
		pageBean.setDetachedCriteria(detachedCriteria);
		
		//不行要返回值  在dao中已经把值都注入pagebaen 对象引用了
		staffService.pageQueryList(pageBean);
		
		//使用json-lib将pagebean对象序列化为json对象返回
		// 说给你json-config 设置不需要返回的值
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[]{"detachedCriteria","currentPage","pageSize","subareas","decidedzones"});
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(json);
		return NONE;
	}
	
	//staff作废操作  将staff 的deltag值设为 1  线根据id查询 staff  在经持久态对象staff的值设为 1 
	//使用属性 驱动接收 ids
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}
	@RequiresPermissions("staff-delete")
	public String doDelete(){
		staffService.doDelete(ids);
		return "listStaff";
	}
	
	//updateStaff 修改 staff
	public String updateStaff(){
		//根据id 查询要修改的项 
		Staff staff =staffService.findById(model.getId());
		staff.setName(model.getName());
		staff.setTelephone(model.getTelephone());
		
		if (model.getHaspda() !=null) {
			staff.setHaspda(model.getHaspda());
		}else {
			staff.setHaspda("0");
		}
		
		staff.setDeltag(model.getDeltag());
		staff.setStandard(model.getStandard());
		staff.setStation(model.getStation());
		staffService.update(staff);
		return "listStaff";
	}
	
	
	//decidedzoneStaffSelect decidedzoneStaffSelect  td>定区选择负责人</td>
	public String decidedzoneStaffSelect(){
		//  使用离线条件对象 指定符合的staff 返回  没有作废的 deltag= 0  没有指定分区的staff中 decidedzones集合为null
		DetachedCriteria dc = DetachedCriteria.forClass(Staff.class);
		dc.add(Restrictions.eq("deltag", "0"));
		dc.add(Restrictions.isEmpty("decidedzones"));
		
		List<Staff> list= staffService.findByCondition(dc);
		
		String[] excludes=new String[]{"decidedzones","telephone","haspda","deltag","station","standard"};
		this.jsonList(list, excludes);
		return NONE;
	}
}



